/******************************************************************************
 **
 **    LISA Confidential
 **
 **    Source Materials
 **
 **    (C) COPYRIGHT LISA 2008-2009
 **
 **
 **  File Name:      TransProcessInstancebyTaskInstanceCommand.java
 **  Description:
 **  Date:           2008-10-28
 **  Author:         LisaQian
 ******************************************************************************/
package com.ti.andon.workflow.command;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmContext;
import org.jbpm.command.Command;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;

import com.ti.andon.workflow.WFConstants;

public class TransProcessInstancebyTaskInstanceCmd implements Command
{
	/**
	 * Serial Version ID.
	 */
	private static final long serialVersionUID = 4748340657431567969L;

	/**
	 * The current taskInstance
	 */
	TaskInstance taskInstance;

	/**
	 * The transition that the taskInstance transits
	 */
	String transition;

	/**
	 * The current userId
	 */
	Long actorId;

	/**
	 * The userId for the actor of the next taskInstance
	 */
	Long nextActorId;

	/**
	 * The comment added by the processor
	 */
	String comment;

	private static final Log logger = LogFactory.getLog(TransProcessInstancebyTaskInstanceCmd.class);

	public TransProcessInstancebyTaskInstanceCmd(TaskInstance taskInstance, String transition, Long nextActorId,
			Long actorId, String comment)
	{
		this.taskInstance = taskInstance;
		this.transition = transition;
		this.nextActorId = nextActorId;
		this.comment = comment;
		this.actorId = actorId;
	}

	/**
	 * Gets the node name of the task instance, and triggers the transition. 
	 * @param jbpmContext 
	 * @return The destination node name of the task instance.
	 * */
	public Object execute(JbpmContext jbpmContext) throws Exception
	{
		try
		{
			if (null != actorId)
			{
				this.taskInstance.setActorId(this.actorId.toString());
			}
			if (null != comment && !comment.equals(""))
			{
				this.taskInstance.addComment(comment);
			}
			// Sets variable.
			ContextInstance contextInstance = this.taskInstance.getContextInstance();
			if (null != nextActorId)
			{
				contextInstance.setVariable(WFConstants.NEXT_REQUESTER, nextActorId);
			}
			if (null != transition)
			{
				contextInstance.setVariable(WFConstants.CURRENT_TRANSITION_NAME, transition);
			}			
			contextInstance.setVariable(WFConstants.TASK_PROCESSED, WFConstants.STRING_YES);

			if (null != transition)
			{
				this.taskInstance.end(transition);
			}
			else
			{
				this.taskInstance.end();
			}
			jbpmContext.save(this.taskInstance);
		}
		catch (Exception e)
		{
			logger.error("TransProcessInstancebyTaskInstanceCmd.execute() : Error occurred : " + e.toString());
			throw (new Exception(e));
		}
		
		return this.taskInstance.getToken().getNode().getName();		
	}

}
